package com.hfb.mashibing.alip8.spring.component;


import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter(filterName = "myFilter", urlPatterns = "/fr")
public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("myFilter is init()");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain filterChain) throws IOException, ServletException {

        System.out.println("myFilter is doFilter()");

        // 常用场景：
        //      是否登录 —— session中是否有user
//        if (((HttpServlet)request.getServletContext()).getServletContext().getAttribute("token") != null) {
//        }

        filterChain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        System.out.println("myFilter is destroy()");
    }
}
